#define new labels for the colnames in all.appeals and provide function to redefine them


colname.label <- rbind(
c("narabs_J" , 'Mixed Panel'),      
c("female_j" , 'Female Judges'),
 c("accused_arab"  , 'Arab Defendant'),
 c("accused_female" , 'Female Defendant'),
 c("criminal_record", 'Previous Criminal Record'),
 c( "Prison_term_M" , 'Prison term, Magistrate Court'),
 c( "Com_term_M"     , 'Community Service, Magistrate Court'),
 c( "Fine_M"         , 'Fine, Magistrate Court'),
 c( "Compensation_M" , 'Victim Compensation, Magistrate Court'),
c("incarceration"  , 'Sent to Prison, District Court'),
c( "Prison_term_D"  ,'Prison term,: District Court'),
c( "Com_term_D"     , 'Community Service, District Court'),
c( "Fine_D"         , 'Fine, District Court'),
c( "Compensation_D", 'Victim Compensation, District Court'),
c( "methodGuilty"   , 'Convicted by guilty plea'),
c( "methodTrial"    , 'Convicted by trial'),
c( 'offense1', "Physical or Sexual Assault"       ),
c( 'offense2', "Property or Fraud"       ),
c( 'offense3', "Fiscal, Economic, or Business"       ),
c(  'offense4', "Regulatory"      ),
c( "victimRace2"    , 'Jewish Victim'),
c( "prequest2"      , 'Prosecution requested rejection of appeal'),
c( "appellant1"     , 'Defendant appealed'),
c( "appellant2"     , 'Prosecution appealed'),
c( "lenient", 'More lenient higher-court verdict'),
c( "harsher", 'Harsher higher-court verdict'),
c( "court_Nazareth" , 'Nazareth Court'),
c( "court_TLV"      , 'Tel Aviv Court'),
c( "court_Jerusalem", 'Jerusalem Court'),
c('avg_jage', 'Average Judge Age'),
c('avg_jexp', 'Average Judge Experience'),
c('marg.eff.jew', 'Marg. Effect of Mixed Panel (Jewish Def.)'),
c('marg.eff.arab', 'Marg. Effect of Mixed Panel (Arab Def.)'),
c('narabs_J:accused_arab', 'Mixed Panel \\times Arab Defendant'),
c( "court_NazarethTRUE" , 'Nazareth Court'),
c( "court_TLVTRUE"      , 'Tel Aviv Court'),
c( "court_JerusalemTRUE", 'Jerusalem Court'),
c('mean.Tr', 'Mean: Mixed'),
c('mean.Co','Mean: All-Jewish'),
c('sdiff','Std. Diff.'),
c('var.ratio','Variance Ratio'),
c('T pval','Paired t-test p-value'),
c('KS pval','KS test p-value'),
c('Fisher_pval', 'Fisher exact p-value'),
c("narabs\\_J" , 'Mixed Panel'),      
c("female\\_j" , 'Female Judges'),
 c("accused\\_arab"  , 'Arab Defendant'),
 c("accused\\_female" , 'Female Defendant'),
 c("criminal\\_record", 'Previous Criminal Record'),
 c( "Prison\\_term\\_M" , 'Prison term, Magistrate Court'),
 c( "Com\\_term\\_M"     , 'Community Service, Magistrate Court'),
 c( "Fine\\_M"         , 'Fine, Magistrate Court'),
 c( "Compensation\\_M" , 'Victim Compensation, Magistrate Court'),                        
c( "Prison\\_term\\_D"  ,'Prison term,: District Court'),
c( "Com\\_term\\_D"     , 'Community Service, District Court'),
c( "Fine\\_D"         , 'Fine, District Court'),                     
c( "court\\_Nazareth" , 'Nazareth Court'),
c( "court\\_TLV"      , 'Tel Aviv Court'),
c( "court\\_Jerusalem", 'Jerusalem Court'),
c('avg\\_jage', 'Average Judge Age'),
c('avg\\_jexp', 'Average Judge Experience'),
c('Pred.Mixed','Prediction for Mixed Panel'),
c('Pred.AllJewish', 'Prediction for All-Jewish Panel'),
c('Diff','Marginal Effect of Mixed Panel'),
c('P-val','P-value')    ,
c('marg.eff.alljewish','Marginal Effect of Race (All-Jewish)'),
c('marg.eff.mixed','Marginal Effect of Race (Mixed Panel)')            
)

names2labels <- function(dat, col.or.row = 'col'){
	if(col.or.row == 'neither'){
		stopifnot(class(dat) == 'character')
		my.names <- dat
	}
	else if(col.or.row == 'col'){
		my.names <- colnames(dat)
	}else if (col.or.row == 'row'){
		my.names <- rownames(dat)
	}else{
		print('Error: bad argument to col.or.row')
		return()
	}
	row.idx <- match(my.names, colname.label[,1])
	idx.for.orig <- c(1:length(my.names))
	if(any(is.na(row.idx))){
		idx.for.orig <- idx.for.orig[-which(is.na(row.idx))]	
		row.idx <- row.idx[-which(is.na(row.idx))]
	} 
	if(col.or.row == 'neither'){
		dat[idx.for.orig] <- colname.label[row.idx,2]
	}else if(col.or.row == 'col'){
		colnames(dat)[idx.for.orig] <- colname.label[row.idx,2]
	}else{
		rownames(dat)[idx.for.orig] <- colname.label[row.idx,2]	
	}
	return(dat)
}